//
//  FoodTypeModel.m
//  Reservation
//
//  Created by CHAN, Wai Man on 19/2/14.
//  Copyright (c) 2014 CHAN, Wai Man. All rights reserved.
//

#import "FoodTypeViewController.h"
#import "RestaurantList_Reservation.h"
#import "CurrentOrderModel.h"
#import "LoadingViewController.h"

@implementation FoodTypeViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.topItem.title = @"Réservation";
    self.view.backgroundColor = [UIColor colorWithRed:0 green:51.0/255 blue:102.255 alpha:1];
    self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:0 green:51.0/255 blue:102.255 alpha:1];
    self.tabBarController.tabBar.backgroundColor = [UIColor colorWithRed:0 green:51.0/255 blue:102.255 alpha:1];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    rootController = self;
    CurrentOrderModel *model = [CurrentOrderModel shareModel];
    [model description];
}

#define CompanyTypeAddress @"http://happywaiman.dlinkddns.com/Reserve/Company/CompanyType.php?type=1"
- (NSArray *)listOfFoodStyle {
    static NSArray *styles = NULL;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [[LoadingViewController shareController] startLoading];
        NSURL *url = [NSURL URLWithString:CompanyTypeAddress];
        NSData *data = [NSData dataWithContentsOfURL:url];
        if (data) {
            NSArray *tempArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:NULL];
            NSMutableArray *container = [NSMutableArray new];
            for (NSArray *array in tempArray) {
                [container addObject:array[1]];
            }
            styles = [NSArray arrayWithArray:container];
        }
        if (styles.count == 0){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Failed" message:@"I can't connect to the server. Please check your network" delegate:NULL cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }
        [[LoadingViewController shareController] stopLoading];
        [self.collectionView reloadData];
    });
    
    return styles;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self listOfFoodStyle].count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FoodTypeViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Style" forIndexPath:indexPath];
    if (cell) {
        NSString *name = [self listOfFoodStyle][indexPath.row];
        
        cell.layer.bounds = cell.bounds;
        cell.layer.borderWidth = 3;
        cell.layer.borderColor = cell.title.textColor.CGColor;
        
        UILabel *textView = cell.title;
        textView.text = NSLocalizedString(name, @"Localize food style").capitalizedString;
        [cell addSubview:textView];
        
    }
    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    UIViewController *con = (UIViewController *)segue.destinationViewController;
    NSIndexPath *path = [self.collectionView indexPathForCell:sender];
    con.title = [self listOfFoodStyle][path.row];
    ((RestaurantList_Reservation *)con).foodType = path;
}

@end


@implementation FoodTypeViewCell
@synthesize title;
@end